clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/****************************************************/
/* WAGES WAGES WAGES adjusted for local price index */
/****************************************************/
capture program drop rlwages
program define rlwages
version 11
syntax, wage(name) occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\final.dta", clear
/*
joinby aze an using "M:\ImmPanelRevis17\Donnees\Loyer\ipc_Local.dta", unm(m)
drop _merge

/* wage adjusted for housing costs */
gen lsnd6=ln((sn/dp)*(100/ipcl6))
gen lsnd20=ln((sn/dp)*(100/ipcl20))
gen lsnd30=ln((sn/dp)*(100/ipcl30))
*/
keep if an==1999 | an==2007
gen lsnd=ln((sn/dp))
gen lsn=ln(sn)
gen dlsn=D.lsn
gen dlsnd=D.lsnd
gen dlsnd6=D.lsnd6
gen dlsnd20=D.lsnd20
gen dlsnd30=D.lsnd30

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* residual wages for each year */
quietly: reg `wage' F_* 
predict rlsnd, residuals
quietly: reg lsnd F_* 
predict rlsnd2, residuals
/* change in residual wages 
gen drlsnd=D.rlsnd if lshift==1
replace drlsnd=D.rlsnd2 if lshift==0
*/
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in   2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)
drop if missing(dlsnd20) | missing(dlsnd30) 

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
joinby ze an using iw2.dta , unm(m)
drop _merge

gen shinter=dimm*lshift
gen sdimm=dimm*(1-lshift)
gen shinter75=dimtm2*lshift
gen ointer=dimm*oshift
gen ointer75=dimtm2*oshift

ivreg2 drlsnd (dimm  = dimtm2 ) Y_* [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c1

ivreg2 drlsnd (dimm shinter  = dimtm2 shinter75 ) Y_* [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c2

ivreg2 drlsnd (dimm shinter  = dimtm2 shinter75 ) lshift Y_* [aweight = iw2] , cl(ze) partial(Y_*)
estimates store c3

estout c1 c2 c3 , cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 widstat) starlevels(* 0.10 ** 0.05 *** 0.01)

esttab c3 ///
using "C:\Users\Public\Documents\Tab21\Table11_Housing.rtf" /// 
, append title("`occp' `wage'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(widstat) star(* 0.10 ** 0.05 *** 0.01)

end

capture rm "C:\Users\Public\Documents\Tab21\Table11_Housing.rtf"

rlwages, wage(lsnd) occp(bc) 
rlwages, wage(lsnd20) occp(bc) 
rlwages, wage(lsnd30) occp(bc)

rlwages, wage(lsnd) occp(bcnq) 
rlwages, wage(lsnd20) occp(bcnq) 
rlwages, wage(lsnd30) occp(bcnq) 
